package livesun.justiceeternal.db.task;


import org.greenrobot.greendao.AbstractDao;

import livesun.justiceeternal.utils.HandlerFactory;

public class AbsUpdateTask<T> extends AbsTemplateDbTask<T> {

    private T mBean;
    private boolean mUpdateSuccess;

    public AbsUpdateTask(Class<? extends T> t, T bean) {
        super(t);
        mBean = bean;
    }

    @Override
    public final void onTaskExecute(AbstractDao<T, ?> dao) {
        if (dao != null) {
            try {
                dao.update(mBean);
                mUpdateSuccess = true;
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        HandlerFactory.main().post(new Runnable() {
            @Override
            public void run() {
                if (mUpdateSuccess) {
                    onFinished(mBean);
                } else {
                    onFail(mBean);
                }
            }
        });
        return;
    }

    public void onFinished(T t) {

    }

    public void onFail(T t) {
    }


}
